<?php

class UsersController extends AdminController
{
    public function indexAction()
    {
        $do = $this->get('do');
        switch ($do) {
            case 'list':
                $this->operation_log('查看后台用户列表');
                $parameters['conditions'] = array();
                $data = $this->_list(AdminUsersModel::class, $parameters);
                foreach ($data['data'] as $item) {
                    $data_result[] = array(
                        'id' => $item->id,
                        'username' => $item->username,
                        'group_name' => ($item->group) ? $item->group->gname : '-',
                        'status' => $item->status
                    );
                }
                $this->ajaxresult(0, '', $data_result, array('count' => $data['count']));
                break;
        }
    }

    /**
     * set password
     */
    public function setpwdAction()
    {
        if ($this->ispost) {
            $old_password = $this->post('old_password');
            $new_password = $this->post('new_password');
            $confirm_password = $this->post('confirm_password');
            if ($old_password && $new_password && $confirm_password && $new_password == $confirm_password) {
                // 检测旧密码
                $user = AdminUsersModel::find($this->user['userinfo']->id);
                if ($user) {
                    if (AdminUsersModel::hash_password($old_password, $user->salt) == $user->password) {
                        $salt = rand(111111, 999999);
                        $user->salt = $salt;
                        $user->password = AdminUsersModel::hash_password($new_password, $salt);
                        $user->save();
                        $this->operation_log('修改密码');
                        $this->ajaxresult(0, '修改成功', array('url' => '/admin/login/out'));
                    } else {
                        $this->ajaxresult(1, '用户旧密码错误');
                    }
                } else {
                    $this->ajaxresult(1, '用户登录有误');
                }
            } else {
                $this->ajaxresult(1, '旧密码及两次输入新密码必须一致');
            }
        }
    }

    /**
     * 保存管理员用户
     */
    public function saveAction()
    {
        $id = $this->request->getParam('id');
        if ($id) {
            $data = AdminUsersModel::find($id);
            if (!$data) $this->ajaxresult(1, '您要更新的账户不存在');
            $this->view->assign('data', $data);
            if ($data->google_secret) {
                // 有KEY
            } else {
                $key = Google2FA::get_secret_key();
                $qrcodeurl = Google2FA::get_qrcode_url($key, $data->username);
                $this->view->assign('qrcodeurl', $qrcodeurl);
                $this->view->assign('google2fa_key', $key);
            }
        }
        if ($this->ispost) {
            $operation_before_data = array();
            $postData = array(
                'username' => $this->post('username'),
                'password' => $this->post('password'),
                'createtime' => time(),
                'status' => $this->post('status') == 'on' ? 1 : 0,
                'groupid' => $this->post('groupid'),
                'avatar' => $this->post('avatar')
            );

            if (empty($postData['groupid'])) {
                $this->ajaxresult(1, '请选择用户所属用户角色分组');
            } elseif (is_null($postData['password']) && !$id) {
                // 新增无密码;
                $this->ajaxresult(1, '新增用户必须填入密码');
            }
            if (!empty($postData['password'])) {
                $postData['salt'] = rand(100000, 999999);
                $postData['password'] = md5(md5($postData['password']) . $postData['salt']);
            }

            // 验证绑定二维码
            $verifycode = $this->post('verifycode');
            $google2fa_key = $this->post('google2fa_key');
            if ($google2fa_key) {
                if ($verifycode) {
                    // 验证
                    if (Google2FA::verify($google2fa_key, $verifycode)) {
                        // 成功
                        $postData['google_secret'] = $google2fa_key;
                    } else {
                        // 验证失败
                        $this->ajaxresult(1, '验证码验证失败');
                    }
                } else {
                    // 验证码为空了
                    //$this->ajaxresult(1, '验证码不能为空');
                }
            }

            if ($data) {
                $operation_before_data = array(
                    'username' => $data->username,
                    'status' => $data->status,
                    'groupid' => $data->groupid,
                    'avatar' => $data->avatar,
                );
                // 更新
                unset($postData['username'], $postData['createtime']);
                if (empty($postData['password'])) {
                    // 新增无密码
                    unset($postData['password']);
                }
                if ($this->user['userinfo']->id == $data->id) unset($postData['status']);
                $status = $data->save($postData);
            } else {
                // 新增
                // 判断账户是否存在
                $hasUser = AdminUsersModel::where(array('username' => $postData['username']))->find();
                if ($hasUser) $this->ajaxresult(1, '用户已存在无法新增');
                $data = new AdminUsersModel();
                $status = $data->save($postData);
            }
            if ($status == false) $this->ajaxresult(1, '用户操作失败');
            $this->operation_log('修改管理员用户', $operation_before_data, $postData);
            $this->ajaxresult(0, '操作成功', array('url' => '/admin/users/index'));
        }
        // 角色分组
        $this->view->assign('groupList', AdminGroupsModel::select());
    }

    public function delAction()
    {
        $id = $this->request->getParam('id');
        if ($id == $this->user['userinfo']->id) $this->ajaxresult(1, '自己无法删除自己的账户');
        if ($id) {
            $data = AdminUsersModel::find($id);
            if ($data) {
                $data->delete();
                $this->operation_log('删除管理员用户', array('id' => $id));
            }
        }
        $this->ajaxresult(0, '操作成功');
    }
}
